﻿using Microsoft.Expression.Drawing.Core;
using project.B12.application.Ioc;
using project.B12.IRepository;
using project.B12.Model.Models;
using project.B12.Model.Res;
using project.B12.Repository.Repository;
using project.B12.ViewModels;
using software.Common.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;

namespace project.B12.application.ViewModels.Setting
{
    public class BacteriaViewModel:ObservableObject
    {
        private IBacteriaRepository _bacteriaRepository;

        private MonitorViewModel _monitorViewModel;

        private PublicResourceViewModel _publicResourceViewModel;

        public RelayCommand Add => new RelayCommand(AddBacteria);

        public RelayCommand<object> Update => new RelayCommand<object>(UpdateBacteria);

        public RelayCommand<object> Delete => new RelayCommand<object>(DeleteBacteria);

        public RelayCommand Query => new RelayCommand(QueryIndistinct);

        public RelayCommand Cancel => new RelayCommand(CancelAdd);

        public ObservableCollection<Table_bacteria> Bacterias {  get; set; }

        private int count;
        public int Count
        {
            get =>count; 
            set { count = value; CurrentPageChanged(); }
        }

        private int countPerPage = 13;
        public int CountPerPage
        {
            get => countPerPage;
            set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
        }

        private int currentPage;
        public int CurrentPage
        {
            get => currentPage;
            set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
        }

        private ObservableCollection<Table_bacteria> _currentBacterias;
        public ObservableCollection<Table_bacteria> CurrentBacterias
        {
            get { return _currentBacterias; }
            set
            {
                _currentBacterias = value;
                this.RaisePropertyChanged();
            }
        }

        private ObservableCollection<string> _cNNames = new ObservableCollection<string>();
        public ObservableCollection<string> CNNames
        {
            get { return _cNNames; }
            set
            {
                _cNNames = value;
                this.RaisePropertyChanged();
            }
        }

        private string _currentName;
        public string CurrentName
        {
            get { return _currentName; }
            set
            {
                _currentName = value;
                this.RaisePropertyChanged();
            }
        }

        private string _nameInput;
        public string NameInput
        {
            get { return _nameInput; }
            set
            {
                _nameInput = value;
                QueryBacterias(value);
                if(string.IsNullOrEmpty(value))
                    CurrentName = null;
                this.RaisePropertyChanged();
            }
        }

        private bool isEnable=true;
        public bool IsEnable
        {
            get { return isEnable; }
            set
            {
                isEnable = value;
                this.RaisePropertyChanged();
            }
        }

        private bool _cancelEnable;
        public bool CancelEnable
        {
            get { return _cancelEnable; }
            set
            {
                _cancelEnable = value;
                this.RaisePropertyChanged();
            }
        }

        public BacteriaViewModel(IBacteriaRepository bacteriaRepository) 
        {
            _bacteriaRepository= bacteriaRepository;
            _publicResourceViewModel= ViewModelLocator.PublicResourceViewModel;
            _monitorViewModel=ViewModelLocator.MonitorViewModel;
            Init();
        }

        public void Init()
        {
            List<Table_bacteria> bacterias = _bacteriaRepository.QueryAllBacteria();
            bacterias.ForEach(item => item.IsAdd = true);
            Bacterias = new ObservableCollection<Table_bacteria>(bacterias);
            Count = Bacterias.Count;
            CurrentBacterias = new ObservableCollection<Table_bacteria>(Bacterias.Take(CountPerPage));
            CurrentPage = 1;
            CNNames = new ObservableCollection<string>();
            CNNames.Add("全部");
            foreach (var bacteria in bacterias)
            {
                CNNames.Add(bacteria.CN_Name);
            }
            _publicResourceViewModel.BacteriaNames = CNNames;
            _publicResourceViewModel.BacteriaNames.Remove("全部");
        }

        private void CurrentPageChanged()
        {
            CurrentBacterias =new ObservableCollection<Table_bacteria>(Bacterias.Skip((CurrentPage - 1) * countPerPage).Take(countPerPage));
        }

        private void AddBacteria()
        {
            if(IsEnable)
            {
                IsEnable = false;
                CancelEnable = true;
                CurrentBacterias.Add(new Table_bacteria());
                return;
            }
            var bacteria = CurrentBacterias.Where(o => o.IsAdd == false).ToList().First();
            if (!ValidateBacteria(bacteria))
            {
                MessageBox.Show(Resources.InformationError);
                return;
            }
            _bacteriaRepository.AddBacterias(bacteria);
            bacteria.IsAdd = true;
            CancelEnable = false;
            IsEnable = true;
            Bacterias.Add(bacteria);
            CNNames.Add(bacteria.CN_Name);
            _publicResourceViewModel.BacteriaNames = CNNames;
            _monitorViewModel.LeftSampleTypes = CNNames.ToList();
            _monitorViewModel.RightSampleTypes = CNNames.ToList();
            MessageBox.Show(Resources.AddSuccess);
        }

        private void CancelAdd()
        {
            if (!IsEnable)
            {
                CurrentBacterias.RemoveLast();
                IsEnable = true;
            }
        }

        private bool ValidateBacteria(Table_bacteria bacteria)
        {
            bool exist = Bacterias.Any(o => o.CN_Name == bacteria.CN_Name);
            if (exist)
                return false;
            if (string.IsNullOrEmpty(bacteria.CN_Name) && string.IsNullOrEmpty(bacteria.EN_Name))
                return false;
            if (string.IsNullOrEmpty(bacteria.Kingdom) ||
                string.IsNullOrEmpty(bacteria.Phylum) ||
                string.IsNullOrEmpty(bacteria.Class) ||
                string.IsNullOrEmpty(bacteria.Order) ||
                string.IsNullOrEmpty(bacteria.Family) ||
                string.IsNullOrEmpty(bacteria.Genus))
                //||string.IsNullOrEmpty(bacteria.Species))
                return false;
            return true;
        }

        private void UpdateBacteria(object bacteria)
        {
            Table_bacteria temp = (Table_bacteria)bacteria;
            int existCount = Bacterias.Where(o => o.CN_Name == temp.CN_Name).Count();
            if (existCount>1)
            {
                MessageBox.Show(Resources.BacteriaUpdateError);
                return;
            }
                
            if (_bacteriaRepository.UpdateBacteria(temp))
            {
                Bacterias.Where(o => o.Id== temp.Id).ToList().First().IsUpdate=true;
                
                MessageBox.Show(Resources.UpdateSucess);
            }
        }

        private void DeleteBacteria(object bacteria)
        {
            MessageBoxResult result = MessageBox.Show(Resources.DeleteWaring, Resources.OperatorConfirm, MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.Cancel)
                return;
            Table_bacteria temp = (Table_bacteria)bacteria;
            temp.IsDelete=true;
            _bacteriaRepository.UpdateBacteria(temp);
            Bacterias.Remove(temp);
            CNNames.Remove(temp.CN_Name);
            _publicResourceViewModel.BacteriaNames = CNNames;
            _monitorViewModel.LeftSampleTypes = CNNames.ToList();
            _monitorViewModel.RightSampleTypes = CNNames.ToList();
            CurrentBacterias.Remove(temp);
            MessageBox.Show(Resources.DeleteSuccess);
        }

        private void QueryBacterias(string name)
        {
            if(name=="全部"||string.IsNullOrEmpty(name))
            {
                //CurrentBacterias = Bacterias;
                CurrentPageChanged();
                return;
            }
                
            var bacterias = Bacterias.Where(o=>o.CN_Name.Contains(name)).ToList();
            CurrentBacterias = new ObservableCollection<Table_bacteria>(bacterias);
        }

        private void QueryIndistinct()
        {
            if (string.IsNullOrEmpty(NameInput))
            {
                CurrentPageChanged();
                return;
            }
            var bacterias = Bacterias.Where(o => o.CN_Name.Contains(NameInput)).ToList();
            CurrentBacterias = new ObservableCollection<Table_bacteria>(bacterias);
        }
    }
}
